<?php

include_once ('Logging.php');

class HybrisClass {

    const WEBSITE_URL = 'http://zhaipei.ganso.com.cn/';     //正式
    const WEBSITE_URL_TEST = 'http://zhaipei.shjieta.com.cn/';//测试

    const API_BASE_URL = 'http://58.40.19.158:9001/ganso';     //正式
//    const API_BASE_URL_TEST = 'http://202.109.115.91:9001/ganso';//测试   'http://202.109.115.91:9001/ganso';  //开发
    const API_BASE_URL_TEST = 'http://58.40.19.230:9001/ganso';                     # 20201022 最新
    const USERNAME_TEST = 'hybris_wsc';
    const USERNAME = 'hybris_ws';
    const PASSWORD = 'hybris_ws';
    const PASSWORD_TEST = 'hybris_wsc';
    private $isDebug = false;

    public function __construct($flag) {
        $this->isDebug=$flag;
    }

    /**
     * hybris token
     */
    private function getToken() {
        $url = (($this->isDebug) ? self::API_BASE_URL_TEST : self::API_BASE_URL) . '/oauth/token';
        $request["username"] = (($this->isDebug) ? self::USERNAME_TEST : self::USERNAME);
        $request["password"] = (($this->isDebug) ? self::PASSWORD_TEST : self::PASSWORD);;
        $request["grant_type"] = "password";
        $request["client_id"] = "pos_ws";
        $request["client_secret"] = "secret";

        $result = $this->http_post($url, $request);
        $result = json_decode($result);

        if (isset($result->access_token)) {
            return $result->access_token;
        }
        return '';
    }

    /*
     * 查询hybris配货单
     */
    public function queryConsignments($storeCode, $startTime,$endTime, $status,$consignmentSync, $acceptFlag ) {
        $token = $this->getToken();
        $url = (($this->isDebug) ? self::API_BASE_URL_TEST : self::API_BASE_URL) . '/v2/Ganso/queryConsignments';

        if (!empty($token)) {

            $data=array();
            $data['storeCode'] = $storeCode;
            $data['startTime'] = $startTime;
            $data['endTime'] = $endTime;

            $data['status'] = $status;
            $data['consignmentSync'] =$consignmentSync;
            $data['acceptFlag'] =$acceptFlag;

            $datas = array();
            $datas['access_token'] = $token;
            $datas['ConsigmentDataList'] = json_encode($data);

            $result =$this->http_post($url, $datas);
            $result = json_decode($result, true);

            return array("result"=>$result,"data"=>json_encode($data));
        }

    }




    /*
  * 依接单时间， 查询hybris配货单
  */
    public function queryZPConsignments($storeCode, $startTime,$endTime, $status,$consignmentSync, $acceptFlag ) {
        $token = $this->getToken();
        $url = (($this->isDebug) ? self::API_BASE_URL_TEST : self::API_BASE_URL) . '/v2/Ganso/queryZPConsignments';

        if (!empty($token)) {

            $data=array();
            $data['storeCode'] = $storeCode;
            $data['startTime'] = $startTime;
            $data['endTime'] = $endTime;

            $data['status'] = $status;
            $data['consignmentSync'] =$consignmentSync;
            $data['acceptFlag'] =$acceptFlag;
            $data['status'] = $status;
            $data['telephone'] = '';
            $data['estimateStartTime'] = '';
            $data['estimateEndTime'] = '';
            $data['pageNum'] = '';
            $data['pageQuantity'] = '';

            $datas = array();
            $datas['access_token'] = $token;
            $datas['ConsigmentDataList'] = json_encode($data);

            $result =$this->http_post($url, $datas);
            $result = json_decode($result, true);

            return array("result"=>$result,"data"=>json_encode($data));
        }

    }



    /**
     * GET 请求
     * @param string $url
     */
    private function http_get($url, $param) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        }
        $strGET = '';
        if (is_string($param)) {
            $strGET = $param;
        } else {
            $aGET = array();
            foreach ($param as $key => $val) {
                $aGET[] = $key . "=" . urlencode($val);
            }
            $strGET = join("&", $aGET);
        }
        $url = empty($strGET)?'':$url . '?' . $strGET;

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
       // curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));

        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);

        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $post_file = false, $contenttype='') {
        $timeout = 8;
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (empty($contenttype)) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
        }else if ($contenttype=="json") {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

        }

        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($oCurl, CURLOPT_TIMEOUT, $timeout);


        $log = new Logging();
        $log->lfile('hybris_http_post');
        $log->lwrite(($url));
        $log->lwrite((json_encode($param)));
        $log->lwrite(($strPOST));

        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        $log->lwrite(($sContent));

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            $url = (($this->isDebug) ? self::WEBSITE_URL_TEST : self::WEBSITE_URL) . '?md=manage&cl=login&at=hybriserror';
            $this->http_get($url, array()) ;
            return false;
        }
    }











}

?>